package com.kbs.im.server.handler;

import com.kbs.im.common.protocol.command.Command;
import com.kbs.im.common.protocol.packet.BasePacket;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息集中分发处理类
 *
 * @author zhangzhang
 */
@ChannelHandler.Sharable
public class IMHandler extends SimpleChannelInboundHandler<BasePacket> {

    public static final IMHandler INSTANCE = new IMHandler();

    private Map<Integer, SimpleChannelInboundHandler<? extends BasePacket>> handlerMap;

    private IMHandler() {
        handlerMap = new HashMap<>();
        handlerMap.put(Command.DEVICE_REGISTER_REQUEST.getCode(), DeviceRegisterRequestHandler.INSTANCE);
        handlerMap.put(Command.MESSAGE_REQUEST.getCode(), MessageRequestHandler.INSTANCE);
        handlerMap.put(Command.LIST_ALL_DEVICE_REQUEST.getCode(), ListAllDeviceRequestHandler.INSTANCE);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BasePacket msg) throws Exception {
        handlerMap.get(msg.getCommand()).channelRead(ctx, msg);
    }
}
